package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.PoStorereqBMapper;
import com.ruoyi.system.service.IBdMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PoStorereqMapper;
import com.ruoyi.system.service.IPoStorereqService;

/**
 * 物资需求申请单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-16
 */
@Service
public class PoStorereqServiceImpl implements IPoStorereqService 
{
    @Autowired
    private PoStorereqMapper poStorereqMapper;

    @Autowired
    private PoStorereqBMapper poStorereqBMapper;

    @Autowired
    private IBdMaterialService iBdMaterialService;
    /**
     * 查询物资需求申请单
     * 
     * @param pkStorereq 物资需求申请单主键
     * @return 物资需求申请单
     */
    @Override
    @DataSource(value = DataSourceType.YONGYOU)
    public PoStorereq selectPoStorereqByPkStorereq(String pkStorereq)
    {
        return poStorereqMapper.selectPoStorereqByPkStorereq(pkStorereq);
    }

    /**
     * 查询物资需求申请单列表
     * 
     * @param poStorereq 物资需求申请单
     * @return 物资需求申请单
     */
    @Override
    @DataSource(value = DataSourceType.YONGYOU)
    public List<PoStorereq> selectPoStorereqList(PoStorereq poStorereq)
    {
        return poStorereqMapper.selectPoStorereqList(poStorereq);
    }

    /**
     * 新增物资需求申请单
     * 
     * @param poStorereq 物资需求申请单
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.YONGYOU)
    public int insertPoStorereq(PoStorereq poStorereq)
    {
        return poStorereqMapper.insertPoStorereq(poStorereq);
    }

    /**
     * 修改物资需求申请单
     * 
     * @param poStorereq 物资需求申请单
     * @return 结果
     */
    @Override
    public int updatePoStorereq(PoStorereq poStorereq)
    {
        return poStorereqMapper.updatePoStorereq(poStorereq);
    }

    /**
     * 批量删除物资需求申请单
     * 
     * @param pkStorereqs 需要删除的物资需求申请单主键
     * @return 结果
     */
    @Override
    public int deletePoStorereqByPkStorereqs(String[] pkStorereqs)
    {
        return poStorereqMapper.deletePoStorereqByPkStorereqs(pkStorereqs);
    }

    /**
     * 删除物资需求申请单信息
     * 
     * @param pkStorereq 物资需求申请单主键
     * @return 结果
     */
    @Override
    public int deletePoStorereqByPkStorereq(String pkStorereq)
    {
        return poStorereqMapper.deletePoStorereqByPkStorereq(pkStorereq);
    }

    @Override
    @DataSource(value = DataSourceType.YONGYOU)
    public List<MarterialVo> getAllPostorereq(String pid) {
        PoStorereq poStorereq = poStorereqMapper.selectPoStorereqByPkStorereq(pid);
        PoStorereqB poStorereqB = new PoStorereqB();
        poStorereqB.setPkStorereq(pid);
        List<PoStorereqB> poStorereqBS = poStorereqBMapper.selectPoStorereqBList(poStorereqB);
        List<MarterialVo> list = new ArrayList<MarterialVo>();
        RequestParam requestParam = new RequestParam();
        for (PoStorereqB storereqB : poStorereqBS) {
            String pkMaterial = storereqB.getPkMaterial();
            requestParam.setPkMaterial(pkMaterial);
            List<MarterialVo> materialList = iBdMaterialService.getMaterialList(requestParam);
            if (materialList != null && materialList.size() > 0){
                materialList.get(0).setNnum(storereqB.getNnum().toPlainString());
                list.add(materialList.get(0));
            }
        }
        return list;
    }

    @Override
    @DataSource(value = DataSourceType.YONGYOU)
    public List<String> getListId() {
        return poStorereqMapper.getListId();
    }
}
